package com.asj.vivo.lock.service.oppoSdk;

import static com.asj.enums.NotificationSwitchEnums.CHANNEL;
import static com.asj.enums.NotificationSwitchEnums.CHANNEL_LOCK_SCREEN;
import static com.asj.enums.NotificationSwitchEnums.CHANNEL_SOUND;
import static com.asj.enums.NotificationSwitchEnums.CHANNEL_STATUS_BAR;
import static com.asj.enums.NotificationSwitchEnums.LOCK_NOTIFICATION;
import static com.asj.enums.NotificationSwitchEnums.VIBRATE;

import android.content.ComponentName;
import android.content.Context;

import com.asj.MainActivity;
import com.asj.annotation.MethodExecute;
import com.asj.annotation.MethodRemark;
import com.asj.exception.CustomRuntimeException;
import com.asj.receiver.MainDeviceAdminReceiver;
import com.google.gson.JsonObject;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceApplicationManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceConnectivityManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceContactManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceControlerManager;
import com.oplus.enterprise.mdmcoreservice.manager.DevicePackageManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceRestrictionManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceSecurityManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceSettingsManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceStateManager;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;

/**
 * OPPO SDK操作集中服务
 */
@Slf4j
public class OPPOSDKService {

    private Context context;
    private MainActivity mainActivity;

    private JsonObject instructObject = null;

    private final ComponentName componentName = new ComponentName("com.asj", "com.asj.MainActivity");


    public static OPPOSDKService getInstance() {
        return new OPPOSDKService();
    }

    public OPPOSDKService(Context context) {
        this.context = context;
    }

    public OPPOSDKService(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }

    public OPPOSDKService(JsonObject instructObject, MainActivity mainActivity) {
        this.mainActivity = mainActivity;
        this.instructObject = instructObject;
    }

    public OPPOSDKService(JsonObject instructObject, Context context) {
        this.context = context;
        this.instructObject = instructObject;
    }

    public OPPOSDKService() {
    }


    //==================================设备权限获取==================================

    /**
     * 设置/取消 EMM 客户端为 Active Admin
     * true-启用;false-禁用
     */
    public void setEmmAdmin(boolean disabled) {
        DeviceSecurityManager deviceSecurityManager = DeviceSecurityManager.getInstance(context);
        try {
            ComponentName adminComponentName = new ComponentName(context, MainDeviceAdminReceiver.class);
            deviceSecurityManager.setEmmAdmin(adminComponentName, disabled);
        } catch (Exception e) {
            throw new CustomRuntimeException("设置/取消 EMM 客户端为 Active Admin 异常:" + e);
        }
    }

    //==================================设备信息获取==================================

    /**
     * 获取IMEI
     */
    @MethodRemark(remark = "获取IMEI", type = 3)
    public String getIMEI() {
        DeviceSecurityManager securityManager = DeviceSecurityManager.getInstance(context);
        return Arrays.toString(securityManager.listImei(componentName));
    }

    /**
     * 查看硬件及系统信息
     * [0]：IMEI/MEID（主卡对应的设备识别码）
     * [1]：IMEI/MEID（副卡对应的设备识别码）
     * [2]：运行内存容量（字符串，单位 GB，如 8GB）
     * [3]：内部存储容量（字符串，单位 GB，如 32GB）
     * [4]：屏幕分辨率（字符串，长*高）
     * [5]：终端生产厂商
     * [6]：终端型号
     * [7]：系统内核版本号
     * [8]：系统软件版本号
     * [9]：安全加固双操作系统版本
     * [10]：系统安全补丁程序级别
     * [11]：ICCID（SIM 卡 1 的 ICCID）
     * [12]：ICCID（SIM 卡 2 的 ICCID）
     * [13]：IMSI（SIM 卡 1 的 IMSI，如果是电信卡返回以逗号分隔的字符串“cdma_imsi,lte_imsi”，否则直接返回 imsi）
     * [14]：IMSI（SIM 卡 2 的 IMSI，如果是电信卡返回以逗号分隔的字符串“cdma_imsi,lte_imsi”，否则直接返回 imsi）
     * [15]：CPU 型号
     * [16]：支持的移动网络制式
     * [17]：无线网卡芯片型号
     * [18]：蓝牙芯片型号
     * [19]：NFC 芯片型号
     * [20]：定位芯片型号
     */
    @MethodRemark(remark = "查看硬件及系统信息", type = 3)
    public String getDeviceInfo() {
        DeviceSecurityManager securityManager = DeviceSecurityManager.getInstance(context);
        List<String> deviceInfo = securityManager.getDeviceInfo(componentName);
        StringBuilder builder = new StringBuilder();
        for (String s : deviceInfo) {
            builder.append(s).append(",");
        }
        return builder.toString();
    }


    /**
     * 获取本机号码
     */
    @MethodRemark(remark = "获取本机号码", type = 3)
    public String PhoneNumber() {
        DeviceSecurityManager securityManager = DeviceSecurityManager.getInstance(context);
        String ka1 = securityManager.getPhoneNumber(componentName, 0);
        if (ka1 == null) {
            ka1 = "无";
        }
        String ka2 = securityManager.getPhoneNumber(componentName, 1);
        if (ka2 == null) {
            ka2 = "无";
        }
        return ka1 + "," + ka2;
    }


    /**
     * 应用耗电量查询
     */
    @MethodRemark(remark = "应用耗电量查询", type = 3)
    public String AppPowerUsage() {
        DeviceStateManager stateManager = DeviceStateManager.getInstance(context);
        List<String[]> data = stateManager.getAppPowerUsage();
        return data.toString();
    }

    /**
     * 终端运行状态查询
     */
    @MethodRemark(remark = "终端运行状态查询", type = 3)
    public String[] GetDeviceState() {
        DeviceStateManager stateManager = DeviceStateManager.getInstance(context);
        return stateManager.getDeviceState();
    }

    /**
     * 软件信息查询
     */
    @MethodRemark(remark = "软件信息查询", type = 3)
    public String GetSoftwareInfo() {
        DeviceStateManager stateManager = DeviceStateManager.getInstance(context);
        String[][] data = stateManager.getSoftwareInfo();
        return Arrays.toString(data);
    }

    /**
     * 系统完整性检测
     */
    @MethodRemark(remark = "系统完整性检测", type = 3)
    public boolean SystemIntegrity() {
        DeviceStateManager stateManager = DeviceStateManager.getInstance(context);
        return stateManager.getSystemIntegrity(componentName);
    }

    /**
     * 查询恢复出厂设置管控状
     */
    @MethodRemark(remark = "查询恢复出厂设置管控状", type = 3)
    public boolean isRestoreFactoryDisabled() {
        DeviceSettingsManager settingsManager = DeviceSettingsManager.getInstance(context);
        return settingsManager.isRestoreFactoryDisabled(componentName);
    }

    //==================================固定屏幕显示==================================

    /**
     * 添加固定屏幕显示应用
     */
    public void addScreenPinningApp(String packName) {
        DeviceApplicationManager applicationManager = DeviceApplicationManager.getInstance(context);
        try {
            applicationManager.addScreenPinningApp(componentName, packName);
        } catch (Exception e) {
            throw new RuntimeException("添加固定屏幕显示应用异常" + e);
        }
    }

    /**
     * 删除固定屏幕显示应用
     */
    public void clearScreenPinningApp(String packName) {
        DeviceApplicationManager applicationManager = DeviceApplicationManager.getInstance(context);
        try {
            applicationManager.clearScreenPinningApp(componentName, packName);
        } catch (Exception e) {
            throw new RuntimeException("删除固定屏幕显示应用 异常:" + e);
        }
    }


    //==================================系统按键操控==================================

    /**
     * 禁用/启用电源键进入关机界面
     */
    public void setPowerDisable(boolean disabled) {
        DeviceRestrictionManager applicationManager = DeviceRestrictionManager.getInstance(context);
        try {
            applicationManager.setPowerDisable(componentName, disabled);
        } catch (Exception e) {
            throw new RuntimeException("禁用/启用电源键进入关机界面 异常" + e);
        }
    }


    //==================================应用软件控制==================================

    /**
     * 删除设备上的该软件
     */
    public void uninstallPackage(String packName) {
        DevicePackageManager devicePackageManager = DevicePackageManager.getInstance(context);
        //默认不保留数据
        try {
            devicePackageManager.uninstallPackage(componentName, packName, false);
        } catch (Exception e) {
            System.out.println("卸载程序异常！" + e);
        }
    }

    //==================================移动网络控制==================================

    /**
     * 设置移动数据管控策略
     * 0:强制关闭终端的移动数据网络，且不允许用户开启;
     * 1:强制开启终端的移动数据网络，且不允许用户关闭;
     * 2:允许用户自主控制终端移动数据网络的开关。
     * 3:关闭移动数据，用户可以手动打开。
     * 4:打开移动数据，用户可以手动关闭。
     */
    @MethodRemark(remark = "设置移动数据管控策略", type = 8)
    public void setMobileDataMode(int mode) {
        DeviceRestrictionManager restrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            restrictionManager.setMobileDataMode(componentName, mode);
            ;
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("设置移动数据管控策略 异常:" + e);
            }
        }
    }

    //==================================WLAN控制==================================

    /**
     * 远程开启/关闭 WLAN
     *
     * @param isChecked 参数写的是String类型，但是实际上是需要Boolean的值，这样做的目的是方便反射设置参数调用方法
     */
    @MethodRemark(remark = "关闭WLAN", type = 8)
    public void WifiInBackground(String isChecked) {
        DeviceRestrictionManager restrictionManager = DeviceRestrictionManager.getInstance(context);
        restrictionManager.setWifiInBackground(componentName, Boolean.parseBoolean(isChecked));
    }

    /**
     * 禁用WLAN
     *
     * @param isChecked 参数写的是String类型，但是实际上是需要Boolean的值，这样做的目的是方便反射设置参数调用方法
     */
    @MethodRemark(remark = "禁用WLAN", type = 8)
    public void setWifiDisabled(String isChecked) {
        DeviceRestrictionManager restrictionManager = DeviceRestrictionManager.getInstance(context);
        restrictionManager.setWifiDisabled(componentName, Boolean.parseBoolean(isChecked));
    }

    /**
     * 设置 WLAN 管控策略
     * 0:底层禁用 WLAN，界面上置灰无法手动操 作;
     * 1:WLAN 关闭且保持一直扫描，但界面上置 灰用户无法手动操作;
     * 2:仅界面上取消置灰，不修改 WLAN 当前状 态，用户可以手动打开或关闭;
     * 3:关闭 WLAN，用户可以手动打开。
     * 4:打开 WLAN，用户可以手动关闭。
     * 5:强制打开 WLAN，用户无法手动关闭，且 在三种省电场景下仍然生效:Doze 模式开 启、省电模式开启、超级省电模式开启。
     */
    @MethodRemark(remark = "设置 WLAN 管控策略", type = 8)
    public void setWlanPolicies(int mode) {
        DeviceConnectivityManager restrictionManager = DeviceConnectivityManager.getInstance(context);
        try {
            boolean resultFlag = restrictionManager.setWlanPolicies(componentName, mode);
            if (!resultFlag) {
                throw new CustomRuntimeException("设置WLAN管控策略 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("设置WLAN管控策略 异常:" + e);
            }
        }
    }

    //==================================蓝牙控制==================================

    /**
     * 禁用/启用蓝牙
     *
     * @param isChecked 参数写的是String类型，但是实际上是需要Boolean的值，这样做的目的是方便反射设置参数调用方法
     */
    @MethodRemark(remark = "禁用蓝牙")
    @MethodExecute
    public void BluetoothDisabled(String isChecked) {
        DeviceRestrictionManager restrictionManager = DeviceRestrictionManager.getInstance(context);
        restrictionManager.setBluetoothDisabled(componentName, Boolean.parseBoolean(isChecked));
    }

    /**
     * 禁止/允许蓝牙文件传输
     *
     * @param isChecked 参数写的是String类型，但是实际上是需要Boolean的值，这样做的目的是方便反射设置参数调用方法
     */
    @MethodRemark(remark = "禁止蓝牙文件传输")
    @MethodExecute
    public void setBluetoothDataTransferDisabled(String isChecked) {
        DeviceRestrictionManager restrictionManager = DeviceRestrictionManager.getInstance(context);
        restrictionManager.setBluetoothDataTransferDisabled(Boolean.parseBoolean(isChecked));
    }

    /**
     * 禁止/允许蓝牙外设通话
     *
     * @param isChecked 参数写的是String类型，但是实际上是需要Boolean的值，这样做的目的是方便反射设置参数调用方法
     */
    @MethodRemark(remark = "禁止蓝牙外设通话")
    @MethodExecute
    public void setBluetoothOutGoingCallDisabled(String isChecked) {
        DeviceRestrictionManager restrictionManager = DeviceRestrictionManager.getInstance(context);
        restrictionManager.setBluetoothOutGoingCallDisabled(Boolean.parseBoolean(isChecked));
    }

    /**
     * 禁止/允许 WLAN 共享网络
     * 1，Android 11 及之后版本：
     * 禁用时，“WLAN 网络共享”、“个人热点”开关处于关闭状态，WLAN 网络共享、个人热
     * 点功能不可用，且该功能界面置灰用户无法手动开启该功能。
     * 允许时，取消上述限制，用户可以手动开启或关闭“WLAN 网络共享”与“个人热点”功能。
     * 2，Android 10 及更早版本：
     * 禁用时，“WLAN 网络共享”开关处于关闭状态，WLAN 网络共享功能不可用，且该功能界
     * 面置灰用户无法手动开启该功能。
     * 允许时，取消上述限制，用户可以手动开启或关闭“WLAN 网络共享”功能。
     */
    @MethodRemark(remark = "禁止WLAN共享网络")
    @MethodExecute
    public void setWifiSharingDisabled(String isChecked) {
        DeviceConnectivityManager deviceConnectivityManager = DeviceConnectivityManager.getInstance(context);
        deviceConnectivityManager.setWifiSharingDisabled(componentName, Boolean.parseBoolean(isChecked));
    }

    /**
     * 禁止/允许蓝牙共享网络
     * 禁用时，“蓝牙共享网络”开关处于关闭状态，蓝牙共享网络功能不可用，且该功能界面置
     * 灰用户无法手动开启该功能。
     * 允许时，取消上述限制，用户可以手动开启或关闭“蓝牙共享网络”功能。
     */
    @MethodRemark(remark = "禁止蓝牙共享网络")
    @MethodExecute
    public void setBluetoothTetheringDisabled(String isChecked) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        deviceRestrictionManager.setBluetoothTetheringDisabled(componentName, Boolean.parseBoolean(isChecked));
    }
    //==================================NFC控制==================================

    /**
     * 禁止/允许 NFC 传输数据
     *
     * @param isChecked true：禁止 AndroidBeam 功能， false：启动 AndroidBeam 功能。
     */
    @MethodRemark(remark = "禁止NFC传输数据")
    @MethodExecute
    public void setAndroidBeamDisabled(String isChecked) {
        DeviceRestrictionManager restrictionManager = DeviceRestrictionManager.getInstance(context);
        restrictionManager.setAndroidBeamDisabled(componentName, Boolean.parseBoolean(isChecked));
    }

    //==================================系统设置控制==================================

    /**
     * 隐藏显示菜单中的某些条目
     * usercenter_preference 登录帐号
     * wifi_settings WLAN
     * network_settings 移动网络
     * bluetooth_settings 蓝牙
     * more_about_network 连接与共享
     * personalized_customization_entrance 个性化定制
     * wallpaper 桌面与锁屏
     * display_and_brightness 显示与亮度
     * sound_and_vibrator 声音与振动
     * notification_and_statusbar 通知与状态栏
     * screen_lock 密码与安全
     * top_level_privacy 权限与隐私
     * key_sos 紧急情况
     * battery_settings 电池
     * application_manager 应用
     * special_function 特色功能
     * personal_assist 小布助手
     * oplus_cloud_service 云服务
     * system_settings 系统设置
     * about_phone 关于本机
     * account_settings 用户与账号
     */
    @MethodRemark(remark = "隐藏显示菜单中的某些条目", type = 8)
    public void setCustomSettingsMenu(String deleteMenus) {
        DeviceControlerManager deviceControlerManager = DeviceControlerManager.getInstance(context);
        try {
            deviceControlerManager.setCustomSettingsMenu(componentName, Arrays.stream(deleteMenus.split(",")).collect(Collectors.toList()));
        } catch (Exception e) {
            throw new RuntimeException("隐藏显示菜单中的某些条目 异常:" + e);
        }
    }

    /**
     * 恢复展示设置菜单中默认条目
     */
    @MethodRemark(remark = "恢复展示设置菜单中默认条目", type = 8)
    public void recoverCustomSettingsMenu() {
        DeviceControlerManager deviceControlerManager = DeviceControlerManager.getInstance(context);
        try {
            deviceControlerManager.recoverCustomSettingsMenu();
        } catch (Exception e) {
            throw new RuntimeException("恢复展示设置菜单中默认条目 异常:" + e);
        }
    }

    /**
     * 设置应用权限
     */
    public void setAppPermission(String packageName, String jsonModel) {
        DeviceSecurityManager deviceSecurityManager = DeviceSecurityManager.getInstance(mainActivity);
        try {
            boolean result;
            if (packageName == null || jsonModel == null) {
                //允许拨打电话权限,获取应用列表权限，发送短信权限，且不允许修改
                String json = "[{\n" +
                        "\t\"permission\": \"SEND_SMS\",\n" +
                        "\t\"mode\": \"ALLOWED\",\n" +
                        "\t\"fixed\": \"true\"\n" +
                        "},{\n" +
                        "\t\"permission\": \"CALL_PHONE\",\n" +
                        "\t\"mode\": \"ALLOWED\",\n" +
                        "\t\"fixed\": \"true\"\n" +
                        "},{\n" +
                        "\t\"permission\": \"ACCESS_BACKGROUND_LOCATION\",\n" +
                        "\t\"mode\": \"ALLOWED\",\n" +
                        "\t\"fixed\": \"true\"\n" +
                        "}{\n" +
                        "\t\"permission\": \"ACCESS_FINE_LOCATION\",\n" +
                        "\t\"mode\": \"ALLOWED\",\n" +
                        "\t\"fixed\": \"true\"\n" +
                        "},{\n" +
                        "\t\"permission\": \"QUERY_ALL_PACKAGES\",\n" +
                        "\t\"mode\": \"ALLOWED\",\n" +
                        "\t\"fixed\": \"true\"\n" +
                        "}]";
                result = deviceSecurityManager.setAppPermission("com.asj", json);
            } else {
                result = deviceSecurityManager.setAppPermission(packageName, jsonModel);
            }
            if (!result) {
                throw new CustomRuntimeException("设置应用权限 失败!");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("设置应用权限 异常:" + e);
            }
        }
    }

    /**
     * 设置可信任应用列表
     * 设置可信任应用列表，列表中的应用静默授予如下权限，用户无法手动修改该应用的权限：1，
     * 悬浮窗；2，应用通知；3，移动数据网络；4，WLAN 无线网络；5，忽略耗电优化。
     */
    @MethodRemark(remark = "设置可信任应用列表", type = 8)
    public void setSuperWhiteList(List<String> packageName) {
        DevicePackageManager packageManager = DevicePackageManager.getInstance(context);
        try {
            boolean resultFlag = packageManager.setSuperWhiteList(componentName, packageName);
            if (!resultFlag) {
                throw new CustomRuntimeException("设置可信任应用列表 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("设置可信任应用列表 异常:" + e);
            }
        }
    }

    /**
     * 从可信任应用列表中删除应用
     */
    @MethodRemark(remark = "从可信任应用列表中删除应用", type = 8)
    public void clearSuperWhiteList(List<String> packageName) {
        DevicePackageManager packageManager = DevicePackageManager.getInstance(context);
        try {
            boolean resultFlag = packageManager.clearSuperWhiteList(componentName, packageName);
            if (!resultFlag) {
                throw new CustomRuntimeException("从可信任应用列表中删除应用 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("从可信任应用列表中删除应用 异常:" + e);
            }
        }
    }

    /**
     * 清空可信任应用列表
     */
    @MethodRemark(remark = "清空可信任应用列表", type = 8)
    public void clearAllSuperWhiteList() {
        DevicePackageManager packageManager = DevicePackageManager.getInstance(context);
        try {
            boolean resultFlag = packageManager.clearAllSuperWhiteList(componentName);
            if (!resultFlag) {
                throw new CustomRuntimeException("清空可信任应用列表 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("清空可信任应用列表 异常:" + e);
            }
        }
    }

    /**
     * 静默允许第三方应用权限
     */
    @MethodRemark(remark = "静默允许第三方应用权限", type = 8)
    public void grantAllRuntimePermission(String packageName) {
        DeviceSecurityManager packageManager = DeviceSecurityManager.getInstance(context);
        try {
            boolean resultFlag = packageManager.grantAllRuntimePermission(packageName);
            if (!resultFlag) {
                throw new CustomRuntimeException("静默允许第三方应用权限 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("静默允许第三方应用权限 异常:" + e);
            }
        }
    }

    /**
     * 禁用/启用恢复出厂设置
     */
    @MethodRemark(remark = "禁止恢复出厂设置")
    public void setRestoreFactoryDisabled(String isDisabled) {
        DeviceSettingsManager settingsManager = DeviceSettingsManager.getInstance(context);
        try {
            boolean resultFlag = settingsManager.setRestoreFactoryDisabled(componentName, Boolean.parseBoolean(isDisabled));
            if (!resultFlag) {
                throw new CustomRuntimeException("禁止恢复出厂设置 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("禁止恢复出厂设置 异常:" + e);
            }
        }
    }

    /**
     * 禁用扩展存储
     */
    @MethodRemark(remark = "禁止扩展存储")
    @MethodExecute

    public void setExternalStorageDisabled(String isChecked) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            deviceRestrictionManager.setExternalStorageDisabled(componentName, Boolean.parseBoolean(isChecked));
        } catch (Exception e) {
            throw new RuntimeException("禁用扩展存储 异常:" + e);
        }

    }

    /**
     * 禁止开发者选项
     *
     * @param disabled true:禁用开发者选项
     *                 false:启用开发者选项
     */
    @MethodRemark(remark = "禁止开发者模式")
    @MethodExecute
    public void setDevelopmentOptionsDisabled(String disabled) {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        try {
            boolean resultFlag = deviceSettingsManager.setDevelopmentOptionsDisabled(componentName, Boolean.parseBoolean(disabled));
            if (!resultFlag) {
                throw new CustomRuntimeException("禁止开发者选项结果 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("禁止开发者选项结果 异常:" + e);
            }
        }
    }

    /**
     * 禁止还原无线设置
     *
     * @param disabled true: 禁用还原无线设置功能
     *                 false: 允许使用还原无线设置功能
     */
    @MethodRemark(remark = "禁止还原无线设置", type = 8)
    public void setNetworkSettingsResetDisabled(String disabled) {
        DeviceConnectivityManager deviceConnectivityManager = DeviceConnectivityManager.getInstance(context);
        try {
            boolean resultFlag = deviceConnectivityManager.setNetworkSettingsResetDisabled(componentName, Boolean.parseBoolean(disabled));
            if (!resultFlag) {
                throw new CustomRuntimeException("禁止还原无线设置 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("禁止还原无线设置 异常:" + e);
            }
        }
    }

    /**
     * 禁止备份恢复
     *
     * @param disabled true：禁用，
     *                 false：允许。
     */
    @MethodRemark(remark = "禁止备份恢复", type = 8)
    public void setBackupRestoreDisabled(String disabled) {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        try {
            boolean resultFlag = deviceSettingsManager.setBackupRestoreDisabled(componentName, Boolean.parseBoolean(disabled));
            if (!resultFlag) {
                throw new CustomRuntimeException("禁止备份恢复 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("禁止备份恢复 异常:" + e);
            }
        }
    }

    /**
     * 禁止使用私密保险箱
     *
     * @param disabled true: 禁用私密保险箱，
     *                 false: 允许使用私密保险箱。
     */
    @MethodRemark(remark = "禁止使用私密保险箱")
    @MethodExecute
    public void setPrivateSafeDisabled(String disabled) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        boolean b = deviceRestrictionManager.setPrivateSafeDisabled(Boolean.parseBoolean(disabled));
        System.out.println("禁止使用私密保险箱：" + b);
    }

    /**
     * 禁止使用应用锁
     * true: 禁用应用锁，
     * false: 允许使用应用锁。
     */
    @MethodRemark(remark = "禁止使用应用锁")
    @MethodExecute
    public void setAppLockDisabled(String disabled) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        deviceRestrictionManager.setAppLockDisabled(Boolean.parseBoolean(disabled));
    }

    /**
     * 擦除数据
     */
    @MethodRemark(remark = "擦除数据", type = 2)
    public void wipeDeviceData() {
        DeviceControlerManager controllerManager = DeviceControlerManager.getInstance(context);
        controllerManager.wipeDeviceData();
    }

    /**
     * 禁止/启用修改壁纸
     *
     * @param isChecked 参数写的是String类型，但是实际上是需要Boolean的值，这样做的目的是方便反射设置参数调用方法
     */
    @MethodRemark(remark = "禁止/启用修改壁纸")
    @MethodExecute
    public void ChangeWallpaperDisable(String isChecked) {
        DeviceRestrictionManager restrictionManager = DeviceRestrictionManager.getInstance(context);
        restrictionManager.setChangeWallpaperDisable(componentName, Boolean.parseBoolean(isChecked));
    }

    /**
     * 禁止/允许用户手动修改时间
     * 禁止/允许用户修改系统时间(日期、时间、时区)。
     * 9.0 及之前版本:禁用/启用更改系统日期和时间设置。禁用时，“自动确定日期和时间”和“自
     * 动确定时区”菜单项开启，且用户不可进行“自动确定日期和时间”和“自动确定时区”菜
     * 单项设置。
     * 9.0 之后版本:禁用/启用更改系统日期和时间设置。禁用时，“自动设置”菜单项开启，且用
     * 户不可进行“自动设置”菜单项设置。
     */
    @MethodRemark(remark = "禁止用户手动修改时间")
    @MethodExecute
    public void setTimeAndDateSetDisabled(String isChecked) {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        deviceSettingsManager.setTimeAndDateSetDisabled(componentName, Boolean.parseBoolean(isChecked));
    }

    /**
     * 禁止/允许用户修改系统语言
     * 禁止/允许用户修改系统语言，如果为禁止状态，会设置当前语言为简体中文。
     */
    @MethodRemark(remark = "禁止用户修改系统语言")
    @MethodExecute
    public void setLanguageChangeDisabled(String isChecked, Context context) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        deviceRestrictionManager.setLanguageChangeDisabled(componentName, Boolean.parseBoolean(isChecked));
    }

    /**
     * 禁止/允许调节屏幕亮度
     * 禁止时：屏幕亮度无法被调节，“设置 - 显示与亮度 - 自动调节亮度”被置灰，用户无法手
     * 动点击。“设置 - 显示与亮度 - 亮度调节滑动条”无法被手动滑动。
     * 状态栏自动亮度按钮被点击后无效果。亮度调节滑动条无法被手动滑动。
     * 通过小布语音无法调节屏幕亮度。
     * 允许时：取消上述限制。
     */
    @MethodRemark(remark = "禁止调节屏幕亮度")
    @MethodExecute
    public void setBrightnessAdjustedEnabled(String isChecked, Context context) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        deviceRestrictionManager.setBrightnessAdjustedEnabled(componentName, Boolean.parseBoolean(isChecked));
    }

    /**
     * 禁止/允许应用分身
     * 禁止时，任何应用均无法使用应用分身功能。对于已开启分身的应用，该分身会被卸载。
     * 允许时，取消此限制。
     * true:分身功能可用， false:禁用分身功能。
     */
    @MethodRemark(remark = "禁止应用分身")
    @MethodExecute
    public void setMultiAppSupport(String isChecked) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        deviceRestrictionManager.setMultiAppSupport(Boolean.parseBoolean(isChecked));
    }

    /**
     * 禁止/允许开启闪光灯
     * 禁止时，用户无法手动开启闪光灯（包括系统应用及三方应用），且快捷设置中“手电筒”功能按钮置灰不可点击。
     * 允许时，取消上述限制。
     *
     * @param mode 0：禁用， 1：启用，
     */
    @MethodRemark(remark = "禁止开启闪光灯")
    @MethodExecute
    public void setTorchPolicies(String mode, Context context) {
        int modeValue = 0;
        if (!Boolean.parseBoolean(mode)) {
            modeValue = 1;
        }
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        deviceRestrictionManager.setTorchPolicies(modeValue);
    }

    /**
     * 设置飞行模式管控策略。
     * 0：DeviceRestrictionManager.AIRPLANE_POLICY_OFF_ FORCE, 强制关闭，用户不可打开
     * 1：DeviceRestrictionManager.AIRPLANE_POLICY_ON_FORCE, 强制打开，用户不可关闭
     * 2：DeviceRestrictionManager.AIRPLANE_POLICY_NO_RESTRICTIONS, 不做限制，常规状态
     * 3：DeviceRestrictionManager.AIRPLANE_POLICY_OFF, 关闭飞行模式，用户可以打开
     * 4：DeviceRestrictionManager.AIRPLANE_POLICY_ON,打开飞行模式，用户可以关闭
     */
    @MethodRemark(remark = "飞行模式策略设置", type = 8)
    @MethodExecute
    public void setAirplanePolices(int mode) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            boolean resultFlag = deviceRestrictionManager.setAirplanePolices(componentName, mode);
            if (!resultFlag) {
                throw new RuntimeException("飞行模式策略设置 失败");
            }
        } catch (Exception e) {
            throw new RuntimeException("飞行模式策略设置 异常:" + e);
        }
    }

    //==================================系统通知控制==================================

    /**
     * 查询某应用总开关
     * 查询某应用总开关
     * */
    @MethodRemark(remark = "打开或关闭某应用总开关",type = 6)
    @MethodExecute
    public void isPackageNotificationEnable(String packageName) {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        try {
            boolean resultFlag = deviceSettingsManager.isPackageNotificationEnable(packageName,false);
            if (!resultFlag) {
                throw new RuntimeException("查询某应用总开关 失败！");
            }
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("查询某应用总开关 异常:" + e);
            }
        }
    }

    /**
     * 打开或关闭某应用总开关
     * 设置打开或关闭某应用总开关
     * */
    @MethodRemark(remark = "打开或关闭某应用总开关",type = 6)
    @MethodExecute
    public void setPackageNotificationEnable(String packageName,boolean enable) {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        try {
            boolean resultFlag = deviceSettingsManager.setPackageNotificationEnable(packageName,false,enable);
            if (!resultFlag) {
                throw new RuntimeException("打开或关闭某应用总开关 失败！");
            }
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("打开或关闭某应用总开关 异常:" + e);
            }
        }
    }

    /**
     * 打开应用所有通知开关
     * 打开指定应用通知整体开关，包括锁屏显示、横幅显示、铃声、振动、免打扰等。 如果有声明其他声音通道，这些声音通道开关也会全部打开
     * */
    @MethodRemark(remark = "打开应用所有通知开关",type = 6)
    @MethodExecute
    public void enableAllNotificationChannel(String packageName) {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        try {
            boolean resultFlag = deviceSettingsManager.enableAllNotificationChannel(packageName);
            if (!resultFlag) {
                throw new RuntimeException("打开应用所有通知开关 失败！");
            }
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("打开应用所有通知开关 异常:" + e);
            }
        }
    }

    /**
     * 关闭应用所有通知开关
     * 关闭指定应用通知整体开关，包括锁屏显示、横幅显示、铃声、振动、免打扰等。 如果有声明其他声音通道，这些声音通道开关也会全部关闭
     * */
    @MethodRemark(remark = "关闭应用所有通知开关",type = 6)
    @MethodExecute
    public void disableAllNotificationChannel(String packageName) {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        try {
            boolean resultFlag = deviceSettingsManager.disableAllNotificationChannel(packageName);
            if (!resultFlag) {
                throw new RuntimeException("关闭应用所有通知开关 失败！");
            }
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("关闭应用所有通知开关 异常:" + e);
            }
        }
    }

    /**
     * 打开/关闭指定应用指定通道指定开关
     * isMultiApp 是否为应用分身（应用分身只默认支持“设置>应用分身”中应用列表，开启应用分身后，将启动一个独立的新应用。如果想要管控包名的分身应用，请传入 true，一般情况下都传 false）
     * channelId 通知通道 ID
     * switchType 通道某项开关，详见 switchType 具体字段
     * enabled 是否打开
     * @param pkgName 应用包名
     */
    @MethodRemark(remark = "打开/关闭指定应用指定通道指定开关", type = 4)
    public void updateNotificationChannel(String pkgName) {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        try {
            //开启通知总开关
            boolean resultFlag = deviceSettingsManager.updateNotificationChannel(pkgName,false,LOCK_NOTIFICATION.getValue(),CHANNEL.getValue(),true);
            if (!resultFlag) {
                throw new CustomRuntimeException("打开/关闭指定应用指定通道指定"+CHANNEL+" 失败！");
            }
            //开启锁屏
            boolean resultFlag02 = deviceSettingsManager.updateNotificationChannel(pkgName,false,LOCK_NOTIFICATION.getValue(),CHANNEL_LOCK_SCREEN.getValue(),true);
            if (!resultFlag02) {
                throw new CustomRuntimeException("打开/关闭指定应用指定通道指定"+CHANNEL_LOCK_SCREEN+" 失败！");
            }
            /*//开启在状态栏上显示
            boolean resultFlag3 = deviceSettingsManager.updateNotificationChannel(pkgName,false,LOCK_NOTIFICATION.getValue(),CHANNEL_STATUS_BAR.getValue(),true);
            if (!resultFlag3) {
                throw new CustomRuntimeException("打开/关闭指定应用指定通道指定"+CHANNEL_STATUS_BAR+" 失败！");
            }
            //开启声音
            boolean resultFlag4 = deviceSettingsManager.updateNotificationChannel(pkgName,false,LOCK_NOTIFICATION.getValue(),CHANNEL_SOUND.getValue(),true);
            if (!resultFlag4) {
                throw new CustomRuntimeException("打开/关闭指定应用指定通道指定"+CHANNEL_SOUND+" 失败！");
            }
            //开启振动
            boolean resultFlag5 = deviceSettingsManager.updateNotificationChannel(pkgName,false,LOCK_NOTIFICATION.getValue(),VIBRATE.getValue(),true);
            if (!resultFlag5) {
                throw new CustomRuntimeException("打开/关闭指定应用指定通道指定"+VIBRATE+" 失败！");
            }*/
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("打开/关闭指定应用指定通道指定开关 异常:" + e);
            }
        }
    }

    /**
     * 指定应用通知通道开关控制
     * 打开或关闭指定应用通知通道开关，通过ConstantUtil中声明的常量， 可以对锁屏显示、横幅显示、铃声、振动、免打扰、信号灯等开关进行控制。
     * isMultiApp 是否为应用分身（应用分身只默认支持“设置>应用分身”中应用列表，开启应用分身后，将启动一个独立的新应用。如果想要管控包名的分身应用，请传入 true，一般情况下都传 false）
     * switchType 通道某项开关，详见 switchType 具体字段
     * enabled 是否打开
     * @param pkgName 应用包名
     */
    @MethodRemark(remark = "指定应用通知通道开关控制", type = 4)
    public void switchNotificationChannel(String pkgName) {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        try {
            //开启锁屏
            boolean resultFlag02 = deviceSettingsManager.switchNotificationChannel(pkgName,LOCK_NOTIFICATION.getValue(),CHANNEL_LOCK_SCREEN.getValue(),true);
            if (!resultFlag02) {
                throw new CustomRuntimeException("打开/关闭指定应用通知通道开关控制"+CHANNEL_LOCK_SCREEN+" 失败！");
            }
            //开启在状态栏上显示
            boolean resultFlag3 = deviceSettingsManager.switchNotificationChannel(pkgName,LOCK_NOTIFICATION.getValue(),CHANNEL_STATUS_BAR.getValue(),true);
            if (!resultFlag3) {
                throw new CustomRuntimeException("打开/关闭指定应用通知通道开关控制"+CHANNEL_STATUS_BAR+" 失败！");
            }
            //开启声音
            boolean resultFlag4 = deviceSettingsManager.switchNotificationChannel(pkgName,LOCK_NOTIFICATION.getValue(),CHANNEL_SOUND.getValue(),true);
            if (!resultFlag4) {
                throw new CustomRuntimeException("打开/关闭指定应用通知通道开关控制"+CHANNEL_SOUND+" 失败！");
            }
            //开启振动
            boolean resultFlag5 = deviceSettingsManager.switchNotificationChannel(pkgName,LOCK_NOTIFICATION.getValue(),VIBRATE.getValue(),true);
            if (!resultFlag5) {
                throw new CustomRuntimeException("打开/关闭指定应用通知通道开关控制"+VIBRATE+" 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("打开/关闭指定应用通知通道开关控制开关 异常:" + e);
            }
        }
    }

    //==================================系统按键控制==================================

    /**
     * 禁止/允许亮屏下按电源键休眠
     * 禁止时，亮屏下按电源键休眠，不息屏不锁屏。
     * 允许时，取消上述管控。
     */
    @MethodRemark(remark = "禁止亮屏下按电源键休眠")
    @MethodExecute
    public void setSleepByPowerButtonDisabled(String isChecked) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            boolean resultFlag = deviceRestrictionManager.setSleepByPowerButtonDisabled(componentName, Boolean.parseBoolean(isChecked));
            if (!resultFlag) {
                throw new RuntimeException("禁止/允许亮屏下按电源键休眠 失败！");
            }
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("禁止/允许亮屏下按电源键休眠 异常:" + e);
            }
        }
    }

    //==================================系统开关机控制==================================

    /**
     * 关机操作
     */
    @MethodRemark(remark = "关机操作", type = 2)
    @MethodExecute
    public void shutdownDevice(String isChecked) {
        //为true 则进行关机
        if (Boolean.parseBoolean(isChecked)) {
            DeviceControlerManager deviceControlerManager = DeviceControlerManager.getInstance(context);
            deviceControlerManager.shutdownDevice(componentName);
        }
    }

    /**
     * 重启操作
     */
    @MethodRemark(remark = "重启操作", type = 2)
    @MethodExecute
    public void rebootDevice(String isChecked) {
        if (Boolean.parseBoolean(isChecked)) {
            DeviceControlerManager deviceControlerManager = DeviceControlerManager.getInstance(context);
            deviceControlerManager.rebootDevice(componentName);
        }
    }

    //==================================音量调节控制==================================

    /**
     * 设置手机静音
     * 设置和取消手机静音，被静音的声音流类型包括：System, Ring, Music, Alarm, Notification。
     * 开启静音模式时，用户无法取消静音，也无法通过物理音量按键调节音量。设置菜单的静音
     * 开关被隐藏，音量调节、音量键默认调节菜单被隐藏。下拉状态栏中静音按钮为开启状态且
     * 不可点击。
     * 取消静音模式时，设置菜单的静音开关、音量调节、音量键默认调节菜单恢复正常显示，下
     * 拉状态栏中静音按钮为关闭状态并恢复可点击状态。用户随后可以通过物理音量按键、音量
     * 设置菜单、下拉状态栏再次手动开启与关闭静音，也可以正常调节音量。
     */
    @MethodRemark(remark = "禁止关闭手机静音")
    @MethodExecute
    public void setVolumeMuted(String isChecked) {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        deviceSettingsManager.setVolumeMuted(componentName, Boolean.parseBoolean(isChecked));
    }

    /**
     * 查询手机是否静音
     */
    @MethodRemark(remark = "查询手机是否静音", type = 3)
    @MethodExecute
    public boolean isVolumeMuted() {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        return deviceSettingsManager.isVolumeMuted(componentName);
    }

    /**
     * 禁用/启用音量调节
     * 当禁用音量调节时，无法通过物理音量键调节音量，用户按键时不弹出音量调节槽，且设置
     * 菜单中音量调节菜单、音量键默认调节菜单被隐藏，用户无法手动设置音量。
     * 注：部分三方应用可以弹出音量调节框，比如腾讯视频、酷狗音乐等，但是音量调节是失败
     * 的，音量大小无变化。
     *
     * @param isChecked 1/true:禁用； 0/false:启用。
     */
    @MethodRemark(remark = "禁用音量调节")
    @MethodExecute
    public void setVolumeChangeActionState(String isChecked) {
        int mode = 0;
        if (Boolean.parseBoolean(isChecked)) {
            mode = 1;
        }
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        deviceSettingsManager.setVolumeChangeActionState(componentName, mode);
    }

    /**
     * 查询音量调节管控状态
     *
     * @return 1：禁止用户手动调节音量； 0：允许用户手动调节音量。
     */
    @MethodRemark(remark = "查询音量调节管控状态", type = 3)
    @MethodExecute
    public int getVolumeChangeActionState() {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        return deviceSettingsManager.getVolumeChangeActionState(componentName);
    }

    //==================================USB设置控制==================================

    /**
     * 禁用/启用 USB 数据传输
     *
     * @param isChecked true 为禁用，false 为启用
     */
    @MethodRemark(remark = "禁用USB数据传输", type = 8)
    @MethodExecute
    public void setUSBDataDisabled(String isChecked) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            deviceRestrictionManager.setUSBDataDisabled(componentName, Boolean.parseBoolean(isChecked));
        } catch (Exception e) {
            throw new RuntimeException("禁用/启用USB数据传输 异常:" + e);
        }
    }

    /**
     * 强制选择USB配置
     *
     * @param policy 可选模式包括：
     *               ConstantUtil.USBTransferPolicy.UNLIMITED：不管
     *               控
     *               ConstantUtil.USBTransferPolicy.CHARGE_ONLY：
     *               仅充电
     *               ConstantUtil.USBTransferPolicy.MTP：MTP（媒体
     *               传输协议）
     *               ConstantUtil.USBTransferPolicy.PTP：PTP（图片传
     *               输协议）
     */
    @MethodRemark(remark = "设置USB配置管控策略", type = 4)
    public void setUSBTransferPolicy(String policy) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            boolean resultFlag = deviceRestrictionManager.setUSBTransferPolicy(policy);
            if (!resultFlag) {
                throw new CustomRuntimeException("设置USB配置管控策略 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("设置USB配置管控策略 异常:" + e);
            }
        }
    }


    //==================================定位服务控制==================================

    /**
     * 设置定位服务管控策略
     *
     * @param policyType 0:禁止终端使用定位服务
     *                   1:强制终端开启定位服务，且不允许关闭
     *                   2:不对定位服务的开关和使用进行控制
     *                   3:关闭定位服务，用户可以手动打开
     *                   4.打开定位服务，用户可以手动关闭
     */
    @MethodRemark(remark = "设置定位服务管控策略", type = 4)
    public void setGpsPolicies(String policyType) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            boolean resultFlag = deviceRestrictionManager.setGpsPolicies(componentName, Integer.parseInt(policyType));
            if (!resultFlag) {
                throw new CustomRuntimeException("设置定位服务管控策略 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("设置定位服务管控策略 异常:" + e);
            }
        }
    }

    /**
     * 查询定位服务管控策略
     *
     * @return int
     * 0:禁止终端使用定位服务
     * 1:强制终端开启定位服务，且不允许关闭
     * 2:不对定位服务的开关和使用进行控制
     * 3:关闭定位服务，用户可以手动打开
     * 4.打开定位服务，用户可以手动关闭
     */
    @MethodRemark(remark = "查询定位服务管控策略", type = 3)
    public int getGpsPolicies() {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        return deviceRestrictionManager.getGpsPolicies(componentName);
    }

    /**
     * 远程打开/关闭定位服务
     *
     * @param disabled true：远程打开定位服务 false：远程关闭定位服务
     */
    @MethodRemark(remark = "打开定位服务", type = 8)
    @MethodExecute
    public void turnOnGPS(String disabled) {
        DeviceConnectivityManager connectivityManager = DeviceConnectivityManager.getInstance(context);
        connectivityManager.turnOnGPS(componentName, Boolean.parseBoolean(disabled));
    }

    /**
     * 查询定位服务是否打开
     *
     * @return true：定位服务开启 false：定位服务关闭
     */
    @MethodRemark(remark = "查询定位服务是否打开", type = 3)
    public boolean isGPSTurnOn() {
        DeviceConnectivityManager connectivityManager = DeviceConnectivityManager.getInstance(context);
        return connectivityManager.isGPSTurnOn(componentName);
    }

    /**
     * 获取当前位置的信息。
     * 当终端有正在使用的当前的位置信息时，可以返回实时的位置。
     */
    @MethodRemark(remark = "获取当前位置信息", type = 3)
    public String getDevicePosition() {
        DeviceConnectivityManager connectivityManager = DeviceConnectivityManager.getInstance(context);
        return connectivityManager.getDevicePosition(componentName);
    }

    //==================================电池与省电设置==================================

    /**
     * 指定应用忽略耗电优化
     * 应用包名，且应用需已安装到设备，否则无效
     *
     * @param enable true 开启忽略，false关闭忽略
     */
    @MethodRemark(remark = "指定应用忽略耗电优化", type = 2)
    @MethodExecute
    public void settingBatteryOptimizations(String packName, boolean enable) {
        DeviceStateManager instance = DeviceStateManager.getInstance(context);
        try {
            if (enable) {
                instance.ignoringBatteryOptimizations(componentName, packName);
            } else {
                instance.allowingBatteryOptimizations(componentName, packName);
            }
        } catch (Exception e) {
            throw new RuntimeException(enable ? "开启" : "关闭" + "指定应用忽略耗电优化 异常！" + e.getMessage());
        }
    }

    //==================================乐划锁屏==================================

    /**
     * 禁止/允许开启乐划锁屏
     * true：禁止开启乐划锁屏功能；
     * false：允许开启乐划锁屏功能。
     */
    @MethodRemark(remark = "禁止/允许开启乐划锁屏", type = 2)
    @MethodExecute
    public void setSystemUpdatePolicies(boolean enabled) {
        DeviceRestrictionManager instance = DeviceRestrictionManager.getInstance(context);
        try {
            instance.setChangePictorialDisable(componentName, enabled);
        } catch (Exception e) {
            throw new RuntimeException("禁止/允许开启乐划锁屏 异常！" + e.getMessage());
        }
    }

    //

    //==================================系统升级管控==================================

    /**
     * 设置系统升级控制策略
     * 0:不允许用户在设置菜单中对终端操作系统进行升级的操作;
     * 1:允许用户在设置菜单中对终端操作系统进行升级的操作。
     */
    @MethodRemark(remark = "设置系统升级控制策略", type = 2)
    @MethodExecute
    public void setSystemUpdatePolicies(int mode) {
        DeviceRestrictionManager instance = DeviceRestrictionManager.getInstance(context);
        boolean flag;
        try {
            flag = instance.setSystemUpdatePolicies(componentName, mode);
        } catch (Exception e) {
            throw new RuntimeException("设置联系人黑白名单管理接口整体开关 异常！" + e.getMessage());
        }
        if (!flag) {
            throw new RuntimeException("设置联系人黑白名单管理接口整体开关 异常！");
        }
    }


    //==================================手机号码黑白名单设置==================================

    /**
     * 设置联系人黑白名单管理接口整体开关
     * true：开
     * false：关
     */
    @MethodRemark(remark = "设置联系人黑白名单管理接口整体开关", type = 2)
    @MethodExecute
    public void setContactBlackListEnable(boolean enable) {
        DeviceContactManager instance = DeviceContactManager.getInstance(context);
        boolean flag;
        try {
            flag = instance.setContactBlackListEnable(componentName, enable);
        } catch (Exception e) {
            throw new RuntimeException("设置联系人黑白名单管理接口整体开关 异常！" + e.getMessage());
        }
        if (!flag) {
            throw new RuntimeException("设置联系人黑白名单管理接口整体开关 异常！");
        }
    }

    /**
     * 设置黑白名单模式
     * 0：普通模式，黑白名单均不生效模式
     * 1：黑名单模式
     * 2：白名单模式
     * 默认值为 0
     * 设置其他值无效
     */
    @MethodRemark(remark = "设置联系人黑白名单模式", type = 2)
    @MethodExecute
    public void setContactBlockPattern(int blockPattern) {
        DeviceContactManager instance = DeviceContactManager.getInstance(context);
        boolean flag = true;
        try {
            flag = instance.setContactBlockPattern(componentName, blockPattern);
        } catch (Exception e) {
            log.error("设置黑白名单模式异常:{}", e.getMessage());
        }
        if (!flag) {
            log.error("设置黑白名单模式异常");
            throw new RuntimeException("设置黑白名单模式异常！");
        }
    }

    /**
     * 设置号码匹配模式
     * 电话号码格式匹配的可选模式：
     * 0:号码全部匹配
     * 1:前缀匹配
     * 2:模糊匹配
     * 3:拦截所有号码（黑名单模式时生效）
     * 4:允许所有号码（白名单模式时生效）
     * 默认值为 0
     * 设置其他值无效
     */
    @MethodRemark(remark = "设置号码匹配模式", type = 2)
    @MethodExecute
    public void setContactMatchPattern(int matchPattern) {
        DeviceContactManager instance = DeviceContactManager.getInstance(context);
        boolean flag = instance.setContactMatchPattern(componentName, matchPattern);
        if (!flag) {
            throw new RuntimeException("设置号码匹配模式 异常！");
        }
    }

    /**
     * 设置通话场景
     * 1.来电场景、接收短信场景
     * 2.去电场景、发送短信场景
     * 3.来去电场景、收发短信场景
     */
    @MethodRemark(remark = "设置通话场景", type = 2)
    @MethodExecute
    public void setContactOutgoOrIncomePattern(int outgoOrIncomePattern) {
        DeviceContactManager instance = DeviceContactManager.getInstance(context);
        boolean flag = instance.setContactOutgoOrIncomePattern(componentName, outgoOrIncomePattern);
        if (!flag) {
            throw new RuntimeException("设置通话场景 异常！");
        }
    }

    /**
     * 添加黑白名单号码列表
     * 1：将号码加入黑名单列表中
     * 2：将号码加入白名单列表中
     */
    @MethodRemark(remark = "添加黑白名单号码列表", type = 2)
    @MethodExecute
    public void addContactBlockNumberList(String phone, int blockPattern) {
        DeviceContactManager instance = DeviceContactManager.getInstance(context);
        int flag = instance.addContactBlockNumberList(componentName, Collections.singletonList(phone), blockPattern);
        if (flag != 0) {
            throw new RuntimeException("添加黑白名单号码列表 异常！");
        }
    }

    /**
     * 删除黑白名单的所有号码列表
     * 1：将号码加入黑名单列表中
     * 2：将号码加入白名单列表中
     */
    @MethodRemark(remark = "删除黑白名单的所有号码列表", type = 2)
    @MethodExecute
    public void removeContactBlockAllNumber(int blockPattern) {
        DeviceContactManager instance = DeviceContactManager.getInstance(context);
        boolean flag = instance.removeContactBlockAllNumber(componentName, blockPattern);
        if (!flag) {
            throw new RuntimeException("删除黑白名单的所有号码列表 异常！");
        }
    }

    //==================================锁屏设置控制==================================

    /**
     * 设备强制锁定
     */
    @MethodRemark(remark = "设备锁屏/解锁", type = 2)
    @MethodExecute
    public void DeviceLocked(String isLocked) {
        try {
            DeviceSecurityManager securityManager = DeviceSecurityManager.getInstance(context);
            if (Boolean.parseBoolean(isLocked)) {
                boolean resultFlag = securityManager.setDeviceLocked(componentName);
                if (!resultFlag) {
                    throw new RuntimeException("设备锁屏失败！");
                }
            } else {
                boolean resultFlag = securityManager.setDeviceUnLocked(componentName);
                if (!resultFlag) {
                    throw new RuntimeException("设备解锁失败！");
                }
            }
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("设备锁屏/解锁 异常!!" + e);
            }
        }
    }

    /**
     * 清除锁屏密
     */
    @MethodRemark(remark = "清除锁屏密码", type = 2)
    @MethodExecute
    public void ClearPassword() {
        DeviceSecurityManager securityManager = DeviceSecurityManager.getInstance(context);
        if (context == null) {
            context = mainActivity;
        }
        ComponentName adminComponentName = new ComponentName(context, MainDeviceAdminReceiver.class);
        securityManager.clearPassword(adminComponentName);
    }

    /**
     * 设置与替换锁屏密码
     */
    @MethodRemark(remark = "设置与替换锁屏密码", type = 4)
    @MethodExecute
    public void setPassword(String isChecked, String screenLockPassword) {
        DeviceSecurityManager securityManager = DeviceSecurityManager.getInstance(context);
        boolean flag = Boolean.parseBoolean(isChecked);
        if (flag) {
            if (context == null) {
                context = mainActivity;
            }
            try {
                ComponentName adminComponentName = new ComponentName(context, MainDeviceAdminReceiver.class);
                boolean resultFlag = securityManager.setPassword(adminComponentName, screenLockPassword);
                if (!resultFlag) {
                    throw new RuntimeException("设置与替换锁屏密码失败！");
                }
                //可能存在其他异常
            } catch (Exception e) {
                if (e instanceof RuntimeException) {
                    throw e;
                } else {
                    throw new RuntimeException("设置与替换锁屏密码错误！" + e.getMessage());
                }
            }
        } else {
            try {
                ClearPassword();
            } catch (Exception e) {
                throw new RuntimeException("清除锁屏密码异常！" + e);
            }
        }
    }

    /**
     * 开启手机屏幕顶层水印并设置文本内容
     */
    @MethodRemark(remark = "开启手机屏幕顶层水印并设置文本内容", type = 5)
    public void setTopWatermarkEnable(String content) {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        try {
            deviceSettingsManager.setTopWatermarkEnable(content);
        } catch (Exception e) {
            throw new RuntimeException("开启手机屏幕顶层水印并设置文本内容 异常：" + e);
        }
    }

    /**
     * 关闭手机屏幕顶层水印
     */
    @MethodRemark(remark = "关闭手机屏幕顶层水印", type = 5)
    public void setTopWatermarkDisable() {
        DeviceSettingsManager deviceSettingsManager = DeviceSettingsManager.getInstance(context);
        try {
            deviceSettingsManager.setTopWatermarkDisable();
        } catch (Exception e) {
            throw new RuntimeException("关闭手机屏幕顶层水印 异常：" + e);
        }
    }

    /**
     * 查询锁屏密码策略
     */
    @MethodRemark(remark = "查询锁屏密码策略", type = 3)
    @MethodExecute
    public String getUserPasswordPolicies() {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        return String.valueOf(deviceRestrictionManager.getUserPasswordPolicies(componentName));
    }

    /**
     * 禁用/启用锁屏界面忘记密码功能
     */
    @MethodRemark(remark = "禁用锁屏界面忘记密码功能")
    @MethodExecute
    public void setDisableKeyguardForgetPassword(String disabled) {
        DeviceControlerManager deviceControlerManager = DeviceControlerManager.getInstance(context);
        try {
            boolean setDisableKeyguardForgetPassword = deviceControlerManager.setDisableKeyguardForgetPassword(componentName, Boolean.parseBoolean(disabled));
            if (!setDisableKeyguardForgetPassword) {
                throw new RuntimeException("锁屏界面忘记密码功能 失败！");
            }
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("锁屏界面忘记密码功能 异常:" + e);
            }
        }
    }

    /**
     * 禁用/启用侧边栏
     * 1，不管控时: 设置-便捷工具-智能侧边栏-按钮功能正常，用户可切换。 如侧边栏开启，桌面侧边显示浮标，可拉出侧边栏。
     * 2，强制开启时: 设置-便捷工具-智能侧边栏-默认开启，用户不可关闭。 桌面始终存在浮标，侧边栏可被拉出。
     * 3，强制关闭时: 设置-便捷工具-智能侧边栏-默认关闭，用户不可开启。 桌面无浮标，无法拉出侧边栏。
     */
    @MethodRemark(remark = "禁用侧边栏")
    @MethodExecute
    public void setSideBarPolicies(String disabled) {
        int mode;
        if (Boolean.parseBoolean(disabled)) {
            mode = 2;
        } else {
            mode = 0;
        }
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            boolean sideBarPolicies = deviceRestrictionManager.setSideBarPolicies(componentName, mode);
            if (!sideBarPolicies) {
                throw new RuntimeException("禁用侧边栏失败！");
            }
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("禁用侧边栏 异常！" + e);
            }
        }
    }


    //==================================常驻后台控制==================================

    /**
     * 添加常驻后台应用
     */
    @MethodRemark(remark = "添加常驻后台应用", type = 5)
    @MethodExecute
    public void addPersistentApp(String packNames) {
        DeviceApplicationManager applicationManager = DeviceApplicationManager.getInstance(context);
        try {
            applicationManager.addPersistentApp(componentName, Collections.singletonList(packNames));
        } catch (Exception e) {
            throw new RuntimeException("添加常驻后台应用异常：" + e);
        }
    }

    /**
     * 移除常驻后台应用
     */
    @MethodRemark(remark = "移除常驻后台应用", type = 5)
    @MethodExecute
    public void removePersistentApp(String packNames) {
        DeviceApplicationManager applicationManager = DeviceApplicationManager.getInstance(context);
        try {
            applicationManager.removePersistentApp(componentName, Collections.singletonList(Objects.requireNonNull(packNames)));
        } catch (Exception e) {
            throw new RuntimeException("移除常驻后台应用异常:" + e);
        }
    }

    /**
     * 查询常驻后台应用
     */
    @MethodRemark(remark = "查询常驻后台应用", type = 3)
    @MethodExecute
    public List<String> getPersistentApp() {
        DeviceApplicationManager applicationManager = DeviceApplicationManager.getInstance(context);
        return applicationManager.getPersistentApp(componentName);
    }

    /**
     * 一键清理后台任务
     */
    @MethodRemark(remark = "一键清理后台任务", type = 5)
    @MethodExecute
    public void cleanBackgroundProcess() {
        DeviceApplicationManager applicationManager = DeviceApplicationManager.getInstance(context);
        try {
            applicationManager.cleanBackgroundProcess(componentName);
        } catch (Exception e) {
            throw new RuntimeException("一键清理后台任务 异常:" + e);
        }
    }


    //==================================应用安装来源控制==================================

    /**
     * 添加可信电子市场
     * ****** 注意 ******
     * 本接口会屏蔽 adb 和 SD 卡安装方式，仅包名添加到 appStorePkgName 列表的应用有安装
     * 其他应用的权限，例如某应用市场。包名不在此列表中的应用不能安装其它应用
     */
    @MethodRemark(remark = "添加可信电子市场", type = 5)
    @MethodExecute
    public void AddTrustedAppStore(String data) {
        DeviceApplicationManager applicationManager = DeviceApplicationManager.getInstance(context);
        applicationManager.addTrustedAppStore(componentName, data);
    }

    /**
     * 删除可信电子市场
     */
    @MethodRemark(remark = "删除可信电子市场", type = 5)
    @MethodExecute
    public void DeleteTrustedAppStore(String data) {
        DeviceApplicationManager applicationManager = DeviceApplicationManager.getInstance(context);
        applicationManager.deleteTrustedAppStore(componentName, data);
    }

    /**
     * 查询可信电子市场列表
     */
    @MethodRemark(remark = "查询可信电子市场列表", type = 3)
    @MethodExecute
    public List<String> GetTrustedAppStore() {
        DeviceApplicationManager applicationManager = DeviceApplicationManager.getInstance(context);
        return applicationManager.getTrustedAppStore(componentName);
    }


    //==================================防卸载应用控制==================================

    /**
     * 添加防卸载应用名单
     */
    @MethodRemark(remark = "添加防卸载应用名单", type = 5)
    @MethodExecute
    public void addDisallowedUninstallPackages(String packNames) {
        DevicePackageManager packageManager = DevicePackageManager.getInstance(context);
        try {
            packageManager.addDisallowedUninstallPackages(componentName, Collections.singletonList(packNames));
        } catch (Exception e) {
            throw new RuntimeException("添加防卸载应用名单异常:" + e);
        }
    }

    /**
     * 删除防卸载应用名单
     */
    @MethodRemark(remark = "删除防卸载应用名单", type = 5)
    @MethodExecute
    public void removeDisallowedUninstallPackages(String packNames) {
        DevicePackageManager packageManager = DevicePackageManager.getInstance(context);
        try {
            packageManager.removeDisallowedUninstallPackages(componentName, Collections.singletonList(packNames));
        } catch (Exception e) {
            throw new RuntimeException("删除防卸载应用名单异常:" + e);
        }
    }

    /**
     * 清空防卸载应用名单
     */
    @MethodRemark(remark = "清空防卸载应用名单", type = 6)
    @MethodExecute
    public void RemoveAllDisallowedUninstallPackages() {
        DevicePackageManager packageManager = DevicePackageManager.getInstance(context);
        packageManager.removeAllDisallowedUninstallPackages(componentName);
    }


    /**
     * 查询所有防卸载应用名单
     */
    @MethodRemark(remark = "查询所有防卸载应用名单", type = 3)
    @MethodExecute
    public List<String> getDisallowUninstallPackageList() {
        DevicePackageManager packageManager = DevicePackageManager.getInstance(context);
        return packageManager.getDisallowUninstallPackageList(componentName);
    }


    //==================================应用启动控制==================================

    /**
     * 添加允许应用启动名单
     * 注意：
     * 1，应用运行白名单开启后，除系统应用和 OPPO 自带应用外，非白名单应用禁止运行，且
     * 白名单中首个应用将被开机启动。
     * 2，若同时配置了应用运行黑、白名单，则黑名单优先级高于白名单，且黑名单可禁用包括系
     * 统应用的所有应用。若同一个应用被同时配置到应用运行黑、白名单中，则该应用无法运行。
     * 3，与 addScreenPinningApp 接口不可同时使用，会出现冲突场景。
     * 4，调试时请注意将调试应用添加到允许应用启动运行名单中，以便调试时取消策略。
     */
    @MethodRemark(remark = "添加允许应用启动名单", type = 5)
    @MethodExecute
    public void addAllowedRunningApp(List<String> packNames) {
        DeviceApplicationManager deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        try {
            boolean allowedRunningApp = deviceApplicationManager.addAllowedRunningApp(packNames);
            if (!allowedRunningApp) {
                throw new CustomRuntimeException("添加允许应用启动名单失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("添加允许应用启动名单异常:" + e);
            }
        }
    }

    /**
     * 删除允许应用启动运行名单
     */
    @MethodRemark(remark = "删除允许应用启动运行名单", type = 5)
    @MethodExecute
    public void removeAllowedRunningApp(List<String> packageNames) {
        DeviceApplicationManager deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        try {
            deviceApplicationManager.removeAllowedRunningApp(packageNames);
        } catch (Exception e) {
            throw new RuntimeException("删除允许应用启动运行名单异常：" + e);
        }
    }

    /**
     * 清除允许应用启动名单
     */
    @MethodRemark(remark = "清除允许应用启动名单", type = 6)
    @MethodExecute
    public void clearAllowedRunningApp() {
        DeviceApplicationManager deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        try {
            deviceApplicationManager.clearAllowedRunningApp();
        } catch (Exception e) {
            throw new RuntimeException("清除允许应用启动名单异常:" + e);
        }
    }

    /**
     * 查询允许应用启动运行名单
     */
    @MethodRemark(remark = "查询允许应用启动运行名单", type = 3)
    @MethodExecute
    public List<String> getAllowedRunningApp() {
        DeviceApplicationManager deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        return deviceApplicationManager.getAllowedRunningApp();
    }


    //==================================Alarm白名单控制==================================

    /**
     * 添加Alarm白名单
     */
    @MethodRemark(remark = "添加Alarm白名单", type = 5)
    @MethodExecute
    public void addAppAlarmWhiteList(String packageNames) {
        DeviceApplicationManager deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        try {
            deviceApplicationManager.addAppAlarmWhiteList(componentName, Collections.singletonList(packageNames));
        } catch (Exception e) {
            throw new RuntimeException("添加Alarm白名单异常:" + e);
        }
    }

    /**
     * 删除Alarm白名单
     */
    @MethodRemark(remark = "删除Alarm白名单", type = 5)
    @MethodExecute
    public void removeAppAlarmWhiteList(String packageNames) {
        DeviceApplicationManager deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        try {
            deviceApplicationManager.removeAppAlarmWhiteList(componentName, Collections.singletonList(packageNames));
        } catch (Exception e) {
            throw new RuntimeException("删除Alarm白名单异常:" + e);
        }
    }

    //======================隐藏指定应用的桌面图标或最近任务显示======================

    /**
     * 隐藏指定应用的桌面图标或最近任务显示
     */
    @MethodRemark(remark = "隐藏指定应用的桌面图标或最近任务显示", type = 7)
    @MethodExecute
    public void setApplicationDisabledInLauncherOrRecentTask(String packageNames, int mode) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            deviceRestrictionManager.setApplicationDisabledInLauncherOrRecentTask(Collections.singletonList(packageNames), mode);
        } catch (Exception e) {
            throw new RuntimeException("隐藏指定应用的桌面图标或最近任务显示 异常:" + e);
        }
    }


    //==================================adb端口控制==================================

    /**
     * 禁止调用adb端口
     */
    @MethodRemark(remark = "禁止调用adb端口")
    @MethodExecute
    public void setAdbDisabled(String disabled) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            deviceRestrictionManager.setAdbDisabled(componentName, Boolean.parseBoolean(disabled));
        } catch (Exception e) {
            throw new RuntimeException("禁止调用adb端口 异常:" + e);
        }
    }

    /**
     * 设置 adb 安装/卸载应用控制策略
     *
     * @param mode 0:允许使用 adb 方式安装/卸载终端应用;
     *             1:不允许使用 adb 方式安装/卸载终端应用。
     */
    @MethodRemark(remark = "设置adb安装/卸载应用控制策略", type = 4)
    @MethodExecute
    public void setAdbInstallUninstallPolicies(int mode) {
        DevicePackageManager devicePackageManager = DevicePackageManager.getInstance(context);
        try {
            boolean resultFlag = devicePackageManager.setAdbInstallUninstallPolicies(mode);
            if (!resultFlag) {
                throw new CustomRuntimeException("设置adb安装/卸载应用控制策略 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("设置adb安装/卸载应用控制策略 异常:" + e);
            }

        }
    }

    //==================================DeviceOwner控制==================================

    /**
     * 设置某个应用为 DeviceOwner
     */
    @MethodRemark(remark = "设置某个应用为 DeviceOwner", type = 5)
    @MethodExecute
    public void setDeviceOwner() {
        DeviceSecurityManager deviceSecurityManager = DeviceSecurityManager.getInstance(context);
        ComponentName adminComponentName = new ComponentName(context, MainDeviceAdminReceiver.class);
        try {
            boolean resultFlag = deviceSecurityManager.setDeviceOwner(adminComponentName);
            if (!resultFlag) {
                throw new CustomRuntimeException("设置应用为 DeviceOwner 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("设置某个应用为 DeviceOwner 异常:" + e);
            }
        }
    }

    /**
     * 取消设置某个应用为 DeviceOwner
     */
    @MethodRemark(remark = "取消设置某个应用为 DeviceOwner", type = 5)
    @MethodExecute
    public void clearDeviceOwner() {
        DeviceSecurityManager deviceSecurityManager = DeviceSecurityManager.getInstance(context);
        try {
            ComponentName adminComponentName = new ComponentName(context, MainDeviceAdminReceiver.class);
            deviceSecurityManager.clearDeviceOwner(adminComponentName.getPackageName());
        } catch (Exception e) {
            throw new RuntimeException("取消设置某个应用为 DeviceOwner 异常:" + e);
        }
    }

    //==================================黑白名单控制==================================

    /**
     * 设置应用安装黑白名单模式
     *
     * @param pattern 0:普通模式(黑白名单都不生效); 1:黑名单(应用限制); 2:白名单(应用无限制)
     */
    @MethodRemark(remark = "设置应用安装黑白名单模式", type = 4)
    @MethodExecute
    public void setAppRestrictionPolicies(int pattern) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            deviceRestrictionManager.setAppRestrictionPolicies(componentName, pattern);
        } catch (Exception e) {
            throw new RuntimeException("设置应用安装黑白名单模式 异常:" + e);
        }
    }

    /**
     * 添加应用安装黑白名单
     *
     * @param pattern 0:普通模式(黑白名单都不生效); 1:黑名单(应用限制); 2:白名单(应用无限制)
     */
    @MethodRemark(remark = "添加应用安装黑白名单", type = 7)
    @MethodExecute
    public void addAppRestriction(int pattern, List<String> packageName) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            deviceRestrictionManager.addAppRestriction(componentName, pattern, packageName);
        } catch (Exception e) {
            throw new RuntimeException("添加应用安装黑白名单 异常:" + e);
        }
    }

    /**
     * 删除应用安装黑白名单
     *
     * @param pattern 0:普通模式(黑白名单都不生效); 1:黑名单(应用限制); 2:白名单(应用无限制)
     */
    @MethodRemark(remark = "删除应用安装黑白名单", type = 7)
    @MethodExecute
    public void removeAppRestriction(int pattern, List<String> packageName) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            deviceRestrictionManager.removeAppRestriction(componentName, pattern, packageName);
        } catch (Exception e) {
            throw new RuntimeException("删除应用安装黑白名单 异常:" + e);
        }
    }

    /**
     * 清空应用安装黑白名单
     *
     * @param pattern 0:普通模式(黑白名单都不生效); 1:黑名单(应用限制); 2:白名单(应用无限制)
     */
    @MethodRemark(remark = "清空应用安装黑白名单", type = 4)
    @MethodExecute
    public void removeAllAppRestriction(int pattern) {
        DeviceRestrictionManager deviceRestrictionManager = DeviceRestrictionManager.getInstance(context);
        try {
            deviceRestrictionManager.removeAllAppRestriction(componentName, pattern);
        } catch (Exception e) {
            throw new RuntimeException("清空应用安装黑白名单 异常:" + e);
        }
    }

    //==================================运行应用名单控制==================================

    /**
     * 添加禁止运行应用名单
     *
     * @param packageNames 待添加的 apk 包名
     */
    @MethodRemark(remark = "添加禁止运行应用名单", type = 5)
    @MethodExecute
    public void addDisallowedRunningApp(List<String> packageNames) {
        DeviceApplicationManager deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        try {
            deviceApplicationManager.addDisallowedRunningApp(componentName, packageNames);
        } catch (Exception e) {
            throw new RuntimeException("添加禁止运行应用名单 异常:" + e);
        }
    }

    /**
     * 删除禁止运行应用名单
     *
     * @param packageNames 待添加的 apk 包名
     */
    @MethodRemark(remark = "删除禁止运行应用名单", type = 5)
    @MethodExecute
    public void removeDisallowedRunningApp(List<String> packageNames) {
        DeviceApplicationManager deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        try {
            deviceApplicationManager.removeDisallowedRunningApp(componentName, packageNames);
        } catch (Exception e) {
            throw new RuntimeException("删除禁止运行应用名单 异常:" + e);
        }
    }

    /**
     * 清空禁止运行应用名单
     */
    @MethodRemark(remark = "清空禁止运行应用名单", type = 6)
    @MethodExecute
    public void removeAllDisallowedRunningApp() {
        DeviceApplicationManager deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        try {
            deviceApplicationManager.removeAllDisallowedRunningApp(componentName);
        } catch (Exception e) {
            throw new RuntimeException("清空禁止运行应用名单 异常:" + e);
        }
    }

    /**
     * 停止应用进程
     * 根据传入应用包名，停止该应用(非系统应用)的所有进程，类似 adb shell am force-stop， 其中包名可以为调用者本身。
     * 包括:停止某个应用的所有进程。
     */
    @MethodRemark(remark = "停止应用进程", type = 5)
    @MethodExecute
    public String killApplicationProcess(String packageName) {
        DeviceApplicationManager deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        try {
            deviceApplicationManager.killApplicationProcess(componentName, packageName);
        } catch (Exception e) {
            System.out.println("停止应用进程 异常:" + e);
            return "停止应用进程 异常";
        }
        return "result_success";
    }

    /**
     * 强行停止指定应用
     * 强行停止指定应用的所有进程，ForceStop 方法。
     */
    @MethodRemark(remark = "强行停止指定应用", type = 5)
    @MethodExecute
    public void forceStopPackage(List<String> packageNames) {
        DeviceApplicationManager deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        try {
            boolean b = deviceApplicationManager.forceStopPackage(componentName, packageNames);
            if (!b) {
                throw new CustomRuntimeException("强行停止指定应用 失败！");
            }
        } catch (Exception e) {
            if (e instanceof CustomRuntimeException) {
                throw e;
            } else {
                throw new RuntimeException("强行停止指定应用 异常:" + e);
            }
        }
    }

    /**
     * 禁用/启用指定应用
     * 禁用应用提供如下模式:
     * 1，启用该应用，应用桌面图标恢复，可以正常启动。
     * 2，禁用指定应用后，该应用被关闭，桌面图标消失，也无法启动。但应用并不被删除。
     * 3，强行停止应用，直到用户实际使用该应用，该应用才会被启动。 同时，此功能需要考虑多用户下的实现，仅在当前用户生效，不影响其它用户
     */
    @MethodRemark(remark = "禁用/启用指定应用", type = 7)
    @MethodExecute
    public String setDisabledAppList(int mode, List<String> packageNames) {
        DeviceApplicationManager deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        try {
            boolean b = deviceApplicationManager.setDisabledAppList(componentName, packageNames, mode);
            if (!b) {
                return "禁用/启用指定应用 异常";
            }
        } catch (Exception e) {
            System.out.println("禁用/启用指定应用 异常:" + e);
            return "禁用/启用指定应用 异常";
        }
        return "result_success";
    }

    /**
     * 查询已被禁用应用的包名列表
     */
    @MethodRemark(remark = "查询已被禁用应用的包名列表", type = 3)
    @MethodExecute
    public List<String> getDisabledAppList() {
        DeviceApplicationManager deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        try {
            return deviceApplicationManager.getDisabledAppList(componentName);
        } catch (Exception e) {
            System.out.println("禁用/启用指定应用 异常:" + e);
            return null;
        }
    }

    /**
     * 开启/关闭飞行模式
     * true:打开 false:关闭
     */
    @MethodRemark(remark = "开启/关闭飞行模式", type = 2)
    public void setAirplaneMode(String on) {
        DeviceControlerManager deviceControlerManager = DeviceControlerManager.getInstance(context);
        deviceControlerManager.setAirplaneMode(componentName, Boolean.parseBoolean(on));
    }

    /**
     * 禁止使用OTG
     * 禁用或启用连接 OTG 外设，且禁用时不可手动开启。
     * true 为禁用，false 为启用
     */
    public void setUSBOtgDisabled(String aTrue) {
        DeviceRestrictionManager deviceApplicationManager = DeviceRestrictionManager.getInstance(context);
        try {
            deviceApplicationManager.setUSBOtgDisabled(componentName, Boolean.parseBoolean(aTrue));
        } catch (Exception e) {
            throw new RuntimeException("禁止使用OTG 异常:" + e);
        }
    }
}
